﻿using System;
using System.ComponentModel.Design;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using CommunityToolkit.Mvvm.Messaging;
using DynamicData;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
using SimpleTool.ActivityUnit;
using SimpleTool.SerialTool.SerialControl;
using SimpleTool.Server;
using SimpleTool.TransformComponent;
using static SimpleTool.SerialTool.SerialControl.CommandSectionModel;
using static SimpleTool.SerialTool.SerialControl.SerialViewModel;

namespace SimpleTool.SerialTool
{
    /// <summary>
    /// OverViewControl.xaml 的交互逻辑
    /// </summary>
    public partial class OverViewControl : UserControl
    {
        private readonly static ILogger Logger = Log.ForContext<OverViewControl>();

        public readonly OverViewModel? HoldOverViewModel;

        public static readonly DependencyProperty ConfigPageProperty = DependencyProperty.Register(
            nameof(ConfigPageType),
            typeof(int),
            typeof(OverViewControl),
            new PropertyMetadata(-1, PageTypeChange));

        public int ConfigPageType
        {
            set
            {
                SetValue(ConfigPageProperty, value);
            }
            get
            {
                return (int)GetValue(ConfigPageProperty);
            }
        }

        public OverViewControl()
        {
            InitializeComponent();
            App MyApp = (App)Application.Current;
            HoldOverViewModel = MyApp.RetailSection.GetRequiredService<OverViewModel>();
            HoldOverViewModel.PowerPlantServer.Read<OverViewControl>(HoldOverViewModel.SerialDataReceive);
            DataContext = HoldOverViewModel;
        }

        public static void PageTypeChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {

            int OldValue = 0;
            OverViewControl self = (OverViewControl)d;
            if (e.OldValue is int ODvalue) OldValue = ODvalue;
            if (e.NewValue is int LastValue)
            {
                if (OldValue != LastValue && self.HoldOverViewModel != null)
                {
                    try
                    {
                        switch (LastValue)
                        {
                            case 2:

                                _ = Task.Run(() =>
                                {
                                    Application.Current.Dispatcher.InvokeAsync(() =>
                                    {
                                        SerialConfigUI UI = new();
                                        self.HoldOverViewModel.ControlContent = UI;
                                    }, DispatcherPriority.Background);
                                });

                                self.HoldOverViewModel.FreeCommandVisibility = Visibility.Visible;
                                break;

                            case 3:

                                _ = Task.Run(() =>
                                {
                                    Application.Current.Dispatcher.InvokeAsync(() =>
                                    {
                                        SerialCommandUI UI = new();
                                        self.HoldOverViewModel.ControlContent = UI;
                                    }, DispatcherPriority.Background);
                                });

                                self.HoldOverViewModel.FreeCommandVisibility = Visibility.Visible;
                                break;

                            case 4:

                                _ = Task.Run(() =>
                                {
                                    Application.Current.Dispatcher.InvokeAsync(() =>
                                    {
                                        HightLightUI UI = new();
                                        self.HoldOverViewModel.ControlContent = UI;
                                    }, DispatcherPriority.Background);
                                });

                                self.HoldOverViewModel.FreeCommandVisibility = Visibility.Collapsed;
                                break;
                        }
                    }
                    catch (Exception error)
                    {
                        Logger.DebudElite(error.Message);
                    }
                }
            }
        }

    }
}
